package com.example.qinghe.frontController.sale;

import com.example.qinghe.entity.communication.Staff;
import com.example.qinghe.entity.sale.Checkinlocations;
import com.example.qinghe.entity.sale.Checkinrecords;
import com.example.qinghe.service.communication.UserService;
import com.example.qinghe.service.sale.CheckinlocationsService;
import com.example.qinghe.service.sale.CheckinrecordsService;
import com.example.qinghe.util.Result;
import com.example.qinghe.util.StrLocation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/front/checkinrecords")
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
@Api(tags = "销售管理->用户签到/签出", description = "FrontCheckinrecordsController")
public class FrontCheckinrecordsController {

    @Autowired
    private CheckinrecordsService checkinrecordsService;

    @Autowired
    private CheckinlocationsService checkinlocationsService;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;


    @ApiOperation("传当前用户的经度(第一个参数)，纬度(第二个参数)过来，进行签到")
    @GetMapping(value = "/CheckIn/{longitude}/{latitude}")
    public Result CheckIn(@PathVariable("longitude") BigDecimal longitude,@PathVariable("latitude") BigDecimal latitude ,Checkinrecords chrecords, HttpServletRequest request){
        Staff staff = userService.getStaff(request);
        if (staff == null)
            return Result.error("请先登录！");
        Checkinlocations checkinlocations = checkinlocationsService.selectCheckinlocationsByStaffIdToday(staff.getId());
        if (checkinlocations==null){
            return Result.error("今天没有该员工签到！");
        }
        Checkinrecords checkinrecords = checkinrecordsService.selectCheckinrecordsByLocationId(checkinlocations.getId());
        if (checkinrecords!=null){
            return Result.success("今天已经签到过了");
        }
        boolean coordinateInRange = StrLocation.isCoordinateInRange(longitude, latitude, checkinlocations.getLongitude(), checkinlocations.getLatitude(), checkinlocations.getRange());
       // 如果签到不在范围
        if (coordinateInRange==false){
            return Result.success("不在签到范围");
        }
        // 到这里签到在范围了
        Checkinrecords checkin=new Checkinrecords();
        checkin.setUserid(staff.getId());
        checkin.setLocationid(checkinlocations.getId());
        checkin.setCheckintime(LocalDateTime.now());

        return checkinrecordsService.CheckIn(checkin)>0?Result.success("签到成功"):Result.success("签到失败");
    }

    @ApiOperation("传当前用户的经度(第一个参数)，纬度(第二个参数)过来，进行签出")
    @GetMapping(value = "/CheckOut/{longitude}/{latitude}")
    public Result CheckOut(@PathVariable("longitude") BigDecimal longitude,@PathVariable("latitude") BigDecimal latitude ,Checkinrecords chrecords, HttpServletRequest request){
        Staff staff = userService.getStaff(request);
        if (staff == null)
            return Result.error("请先登录！");
        Checkinlocations checkinlocations = checkinlocationsService.selectCheckinlocationsByStaffIdToday(staff.getId());
        if (checkinlocations==null){
            return Result.error("今天没有该员工签到！");
        }
        Checkinrecords checkinrecords = checkinrecordsService.selectCheckinrecordsByLocationId(checkinlocations.getId());
        if (checkinrecords==null){
            return Result.success("今天还没有签到");
        }
        if (checkinrecords.getCheckouttime()!=null){
            return Result.success("今天已经签出过了");
        }
        boolean coordinateInRange = StrLocation.isCoordinateInRange(longitude, latitude, checkinlocations.getLongitude(), checkinlocations.getLatitude(), checkinlocations.getRange());
        // 如果签出不在范围
        if (coordinateInRange==false){
            return Result.success("不在签出范围");
        }
        // 到这里签出在范围了

        return checkinrecordsService.CheckOut(staff.getId(), LocalDateTime.now())>0?Result.success("签出成功"):Result.success("签出失败");
    }


}
